﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenGado.DATA;
using OpenGado.DATA.Entities;
using OpenGado.Business;

namespace OpenGado.Business
{
   public  class ProductBusiness
    {
       /// <summary>
       /// Method issues the products based on the request if no request is provided one is generated on the fly.
       /// </summary>
       /// <param name="issueItemHeader"></param>
       /// <param name="requestItemList"></param>
       public void RequestProducts (IssueItemHeader issueItemHeader, List<RequestItem> requestItemList)
       {
           issueItemHeader.RequestStatus = "Open";

           try
           {
               IssueItemsDataControl.Instance.Save(issueItemHeader, requestItemList);

           }
           catch(Exception ex)
           {
               
           }

       }
       /// <summary>
       /// Generates the request number 
       /// </summary>
       /// <returns></returns>
       public string GetRequestNumber(String projectCode)
       {
           String requestNumber = String.Empty;

           int nextRequestId = IssueItemsDataControl.Instance.GetNextId();

           requestNumber = projectCode + nextRequestId.ToString();

           return requestNumber;
       }
       /// <summary>
       /// Method issues the products based on the request.
       /// </summary>
       /// <param name="issueItemHeader"></param>
       /// <param name="issueItemsList"></param>
       public void IssueProducts(IssueItemHeader issueItemHeader, List<IssueItem> issueItemsList)
       {
           try
           {
               issueItemHeader.RequestStatus = "Complete";

               IssueItemsDataControl.Instance.SaveHeader(issueItemHeader);

               Product product;
               Decimal productQuantity;
               Decimal issuedQuantity; 

              foreach (IssueItem item in issueItemsList)
              {
                  product = item.Product;
                  // deduct from stock );
                  issuedQuantity = (Decimal) item.IssueQuantity;
                  productQuantity = (Decimal)product.ProductQuantity;

                  product.ProductQuantity = (productQuantity - issuedQuantity);

                  // update the product quantity

                  ProductDataControl.Instance.Save(product);
                  // write to issues table 
                  IssueItemsDataControl.Instance.Save(item);
              }
             
           }
           catch (Exception)
           {
               throw; 
               
           }


       }
       /// <summary>
       /// Saves product and assigns the project to it.
       /// </summary>
       /// <param name="product"></param>
       /// <param name="project"></param>
        public void Save(Product product, Project project)
        {
          
        }

       /// <summary>
       /// Saves product
       /// </summary>
       /// <param name="product"></param>
       public void Save(Product product)
       {
           ProductDataControl.Instance.Save(product);
       }

       /// <summary>
       /// Deletes product
       /// </summary>
       /// <param name="product"></param>
        public void Delete (Product product)
        {
            ProductDataControl.Instance.Delete(product);
        }

        public Product GetProduct(int productId)
        {

            return ProductDataControl.Instance.GetById(productId);
        }

       public IList<Product> GetAllProducts ()
       {

           return ProductDataControl.Instance.GetAll();

       }

       public void TransferToAnotherProject(Product product, Project project)
        {
            
        }

       public IList<Product> GetProductsByCriteria(String fieldName, String fieldValue)
       {

           return ProductDataControl.Instance.Query(fieldName, fieldValue);
       }



    }
}
